package com.ck.ips.service.impl.sys;

import com.ck.common.model.OperationResult;
import com.ck.common.page.PageInfo;
import com.ck.core.constant.PageConstant;
import com.ck.ips.dao.sys.BrandMapper;
import com.ck.ips.model.vo.sys.BrandVO;
import com.ck.ips.service.sys.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author XC17125
 */
@Service(value = "brandService")
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void addBrand(BrandVO brand) {
        if (brand == null || StringUtils.isEmpty(brand.getBrandName())) {
            throw new IllegalArgumentException("brandService.addBrand参数异常，请检查参数");
        }
        //直接插入数据库，若确认品牌已经存在则返回给前台
        try {
            brandMapper.addBrand(brand);
        } catch (Exception e) {
            //品牌名称已存在
            if (e instanceof DuplicateKeyException) {
                throw new IllegalArgumentException("品牌名称已存在");
            }
            throw e;
        }
    }

    @Override
    public OperationResult editBrand(BrandVO brand) {
        if (brand == null || brand.getBrandId() == null) {
            throw new IllegalArgumentException("brandService.editBrand参数异常，请检查参数");
        }
        try {
            int result = brandMapper.editBrand(brand);
            if (result != 1) {
                return OperationResult.newFailOperationResult("没有找到对应的品牌信息，brandId=" + brand.getBrandId());
            }
            return OperationResult.newSuccessOperationResult();
        } catch (Exception e) {
            //品牌名称已存在
            if (e instanceof DuplicateKeyException) {
                return OperationResult.newFailOperationResult("品牌名称已存在:" + brand.getBrandName());
            }
            throw e;
        }
    }

    @Override
    public OperationResult delBrand(BrandVO brand) {
        if (brand == null || brand.getBrandId() == null) {
            throw new IllegalArgumentException("brandService.delBrand参数异常，请检查参数");
        }
        //检查对应的品牌是否关联了广告，如果还有广告关联则不允许删除 TODO

        //删除品牌信息
        int result = brandMapper.delBrand(brand);
        if (result != 1) {
            return OperationResult.newFailOperationResult("没有找到对应的品牌信息，brandId=" + brand.getBrandId());
        }
        return OperationResult.newSuccessOperationResult();
    }

    @Override
    public OperationResult listBrand(BrandVO brand) {
        int pageNum = PageConstant.DEFAULT_PAGE_NUM;
        int pageSize = PageConstant.DEFAULT_PAGE_SIZE;
        //设置默认值
        if (brand.getPageNum() != null) {
            pageNum = brand.getPageNum();
        }
        if (brand.getPageSize() != null) {
            pageSize = brand.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<BrandVO> brandVOS = brandMapper.listBrand(brand);
        return OperationResult.newSuccessOperationResult(PageInfo.convert(brandVOS));
    }

    @Override
    public OperationResult listAllBrands() {
        List<BrandVO> brandVOS = brandMapper.listAllBrands();
        return OperationResult.newSuccessOperationResult(brandVOS);
    }


    @Override
    public XSSFWorkbook exportBrandExcel(String brandName, String contactPerson, String phoneNum) {
        BrandVO bean = new BrandVO();
        bean.setBrandName(brandName);
        bean.setContactPerson(contactPerson);
        bean.setPhoneNum(phoneNum);
        Page<BrandVO> brandVOS = brandMapper.listBrand(bean);
        PageInfo pageInfo = PageInfo.convert(brandVOS);
        List<BrandVO> resList = pageInfo.getList();
        //构造excel并赋值
        //表头数据
        String[] header = {"编号", "品牌名称", "联系人", "联系方式", "地址", "创建时间"};
        //声明一个工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //生成一个表格，设置表格名称
        XSSFSheet sheet = workbook.createSheet("品牌列表");
        //设置表格列宽度为20个字节
        sheet.setDefaultColumnWidth(30);
        //创建第一行表头
        XSSFRow headrow = sheet.createRow(0);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            XSSFCell cell = headrow.createCell(i);
            //创建一个内容对象
            XSSFRichTextString text = new XSSFRichTextString(header[i]);
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }
        //向excel表中装入数据
        Integer i = 0;
        for (BrandVO brand : resList) {
            i++;
            int columnIndex = 0;
            //建一行
            XSSFRow headrow1 = sheet.createRow(i);
            //第几行
            XSSFCell cell = headrow1.createCell(columnIndex++);
            XSSFRichTextString text = new XSSFRichTextString("" + i);
            cell.setCellValue(text);
            //品牌名称
            cell = headrow1.createCell(columnIndex++);
            text = new XSSFRichTextString(brand.getBrandName());
            cell.setCellValue(text);
            //联系人
            cell = headrow1.createCell(columnIndex++);
            text = new XSSFRichTextString(brand.getContactPerson());
            cell.setCellValue(text);
            //联系方式
            cell = headrow1.createCell(columnIndex++);
            text = new XSSFRichTextString(brand.getPhoneNum());
            cell.setCellValue(text);
            //地址
            cell = headrow1.createCell(columnIndex++);
            text = new XSSFRichTextString(brand.getAddress());
            cell.setCellValue(text);
            //建一个单元格 是否空刊
            cell = headrow1.createCell(5);
            text = new XSSFRichTextString(brand.getAddTimeStr());
            cell.setCellValue(text);
        }
        return workbook;
    }
}
